package com.song.exception.controller;


import com.song.anno.Log;
import com.song.pojo.Dept;
import com.song.pojo.Result;
import com.song.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Lazy
//@Scope("prototype")
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    public DeptController() {
        log.info("创建DeptController实例");
    }
//    private static final Logger log = LoggerFactory.getLogger(DeptController.class);
    @Autowired
    private DeptService deptService;

//    @RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping
    public Result list() {
//        System.out.println("查询全部部门信息/depts");
        log.info("查询全部部门信息/depts");
        List<Dept> depts = deptService.findAll();
        return Result.success(depts);
    }

//    @DeleteMapping("/depts")
//    public Result deletDept(@RequestParam("id") Integer id) {
//        System.out.println("删除部门信息/depts"+id);
//        deptService.deleteById(id);
//        return Result.success();
//    }
    @Log
    @DeleteMapping
    public Result deletDept(Integer id) {
//        System.out.println("删除部门信息/depts:"+id);
        log.info("删除部门信息/depts:"+id);
        deptService.deleteById(id);
        return Result.success();
    }
    @Log
    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
//        System.out.println("添加部门信息/depts----------->"+dept);

        log.info("添加部门信息/depts----------->"+dept);
        deptService.addDept(dept);
        return Result.success();
    }

//    修改部门信息
    @Log
    @PutMapping
    public Result updateDept(@RequestBody Dept dept) {
//        System.out.println("修改部门信息/depts----------->"+dept);
        log.info("修改部门信息/depts----------->{}",dept);
        deptService.update(dept);
        return Result.success();
    }
//    根据id查询部门信息
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
//        System.out.println("查询部门信息/depts----------->"+id);
        log.info("查询部门信息/depts----------->{}",id);
        return Result.success(deptService.findById(id));
    }
}
